import axios from "../miniprogram_npm/axios/index";
import mpAdapter from "../miniprogram_npm/axios-miniprogram-adapter/index";
// import AxiosRequestConfig from "../miniprogram_npm/axios-miniprogram-adapter/index";
axios.defaults.adapter = mpAdapter;
//全局请求加载
let loadingNum: number = 0;
// 开始加载
function startLoading() {
  if (loadingNum == 0) {
    wx.showLoading({
      title: "加载中...",
    });
  }
  loadingNum++;
}
function endLoading() {
  loadingNum--;
  if (loadingNum <= 0) {
    wx.hideLoading();
  }
}
// 创建 axios 实例
const request = axios.create({
  baseURL: "http://192.168.2.174:8080/MobileLinkCenter", // 所有的请求地址前缀部分
  // baseURL:"https://www.hbdinkon.cn:10020/MobileLinkCenter",
    // timeout: 60000, // 请求超时时间毫秒
  // withCredentials: true, // 异步请求携带cookie
});
// 请求拦截器
request.interceptors.request.use(
  function (config: any) {
    startLoading();
    // 统一设置用户身份 token
    //  xxxxxxx
    //设置请求头
    // let token = wx.getStorageSync('token')
    if (config.method == "post") {
      config.headers["Content-Type"] = "application/x-www-form-urlencoded";
    }
    // if(token){
    // config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    // }
    // console.log(config);
    return config;
  },
  function (error: any) {
    return Promise.reject(error);
  }
);
// 响应拦截器
request.interceptors.response.use(
  function (response: any) {
    endLoading();
    // 统一处理接口响应错误，如 token 过期无效、服务端异常等
    if (response.data.status && response.data.status !== 200) {
      return Promise.reject(response.data);
    }
    return response;
  },

  function (error: any) {
    endLoading();
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          wx.showToast({
            title: "错误请求",
            icon: "error",
            duration: 2000,
          });
          break;
        case 401:
          wx.showToast({
            title: "未授权，请重新登录",
            icon: "error",
            duration: 2000,
          });
          break;
        case 403:
          wx.showToast({
            title: "拒绝访问",
            icon: "error",
            duration: 2000,
          });
          break;
        case 404:
          wx.showToast({
            title: "未找到资源",
            icon: "error",
            duration: 2000,
          });
          break;
        case 405:
          wx.showToast({
            title: "请求方法未允许",
            icon: "error",
            duration: 2000,
          });
          break;
        case 408:
          wx.showToast({
            title: "请求超时",
            icon: "error",
            duration: 2000,
          });
          break;
        case 500:
          wx.showToast({
            title: "服务器端出错",
            icon: "error",
            duration: 2000,
          });
          break;
        default:
          console.log(`连接错误${error.response.status}`);
      }
    } else {
      wx.showToast({
        title: "服务器错误",
        icon: "error",
        duration: 2000,
      });
    }
    return Promise.reject(error);
  }
);
/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url: string, params?: any) {
  return new Promise((resolve, reject) => {
    request
      .get(url, {
        params: params,
      })
      .then((response: any) => {
        resolve(response.data);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url: string, data?: any) {
  return new Promise((resolve, reject) => {
    request.post(url, data).then(
      (response: any) => {
        resolve(response.data);
      },
      (err: any) => {
        reject(err);
      }
    );
  });
}
export { request };
